package com.springcloud.zuul.controller;

import com.google.common.collect.Lists;
import com.springcloud.zuul.common.annotation.SysLog;
import com.springcloud.zuul.common.utlis.JsonResult;
import com.springcloud.zuul.entity.SysProductEntity;
import com.springcloud.zuul.service.SysProductMenuService;
import com.springcloud.zuul.service.SysProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: springcloud-zuul
 * @description: 产品
 * @author: zhlu
 * @create: 2019-12-31 15:26
 * @Introduction(说明背景故事)
 **/
@RestController
@RequestMapping("/sys/product")
@Api(tags = "产品管理")
@Slf4j
public class SysProductController extends AbstractController {

    @Autowired
    private SysProductService sysProductService;

    @Autowired
    private SysProductMenuService sysProductMenuService;

    /**
     * Description:获取产品信息
     * @author zhlu
     * @date 2020/1/3 10:39 上午
     * @param productId
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    @GetMapping("/info/{productId}")
    @RequiresPermissions("sys:product:info")
    @ApiOperation(notes = "获取产品信息",value = "获取产品信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "productId",value = "产品编码",required = true,dataType = "String",paramType = "path")
    })
    public JsonResult info(@PathVariable String productId){
        SysProductEntity productEntity=sysProductService.queryByProductId(productId);
        productEntity.setMenuList(sysProductMenuService.queryMenuIdList(productEntity.getProductId()));
        return JsonResult.success(productEntity);
    }

    /**
     * Description:获取所有产品列表
     *
     * @param
     * @return com.springcloud.zuul.common.utlis.JsonResult
     * @author zhlu
     * @date 2019/12/31 3:45 下午
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:product:list")
    @ApiOperation(notes = "查看产品列表", value = "查看产品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keywords",value = "产品名称",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "pageNum",value = "第几页",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页多少条",required = true,dataType = "String",paramType = "query")
    })
    public JsonResult getProductList(@RequestParam Map<String,Object>params) {
        return JsonResult.success(sysProductService.getProductList(params));
    }

    /**
     * Description:保存产品
     * @author zhlu
     * @date 2019/12/31 5:47 下午
     * @param productEntity
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    @SysLog("保存产品")
    @PostMapping("/save")
    @ApiOperation(notes = "保存产品",value = "保存产品")
    public JsonResult save(@RequestBody SysProductEntity productEntity){
        return sysProductService.saveProduct(productEntity);
    }

    /**
     * Description:删除产品
     * @author zhlu
     * @date 2020/1/2 2:33 下午
     * @param produId
     * @return com.springcloud.zuul.common.utlis.JsonResult
     */
    @ApiOperation(notes = "删除产品",value = "删除产品")
    @SysLog("删除产品")
    @PostMapping("/delete/{produId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId",value = "产品编码",required = true,dataType = "String",paramType = "path")
    })
    public JsonResult delete(@PathVariable String produId){
        sysProductMenuService.deleteBatch(Lists.newArrayList(produId));
        return JsonResult.success();
    }


}
